package com.mapproject.industrymap.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mapproject.industrymap.entity.SysUploadFileEntity;
import com.mapproject.industrymap.entity.SysUserEntity;
import com.mapproject.industrymap.mapper.SysUserMapper;
import com.mapproject.industrymap.service.SysUserService;
import com.mapproject.industrymap.utils.EncodeMd5;
import com.mapproject.industrymap.utils.JwtUtil;
import com.mapproject.industrymap.utils.ReturnValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUserEntity> implements SysUserService {
    @Autowired
    SysUserMapper sysUserMapper;
    public ReturnValue createUser(SysUserEntity sysUserEntity){
        String rowguid = UUID.randomUUID().toString(); // 主键uuid
        String systemdate=getsystemdate();
        String usertype=sysUserEntity.getUsertype();
        String phonenum=sysUserEntity.getPhonenum();
        int userid=0;
        QueryWrapper<SysUserEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper
                .orderByDesc("userid")
                .last("limit 1");
        List<Map<String, Object>> maps = sysUserMapper.selectMaps(queryWrapper);
        if(maps.size()==0){
            userid=userid+1;
        }else {
            userid=(int)maps.get(0).get("userid")+1;
        }
        SysUserEntity fileInfo=SysUserEntity.builder()
                .rowguid(rowguid)
                .username(sysUserEntity.getUsername())
                .userid(userid)
                .password(sysUserEntity.getPassword())
                .inserttime(systemdate)
                .modifytime(systemdate)
                .usertype(usertype)
                .phonenum(phonenum)
                .status("1")
                .build();
        boolean result =saveOrUpdate(fileInfo);
        return new ReturnValue(200,"是否创建成功",result);
    }
    public String getsystemdate(){
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss ");
        return sdf.format(d);
    }
    public ReturnValue removeUser(int userid){
        SysUserEntity sysUserEntity=new SysUserEntity();
        sysUserEntity.setStatus("0");
        try{
            UpdateWrapper<SysUserEntity> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("userid",userid);
            int ret=  sysUserMapper.update(sysUserEntity,updateWrapper);
            return  new ReturnValue(200,"删除成功",ret);
        }catch (Exception ex){
            return  new ReturnValue(500,"删除失败");
        }

    }
    public ReturnValue modifyUser(SysUserEntity sysUserEntity){
        sysUserEntity.setModifytime(getsystemdate());

        try{
            UpdateWrapper<SysUserEntity> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("userid",sysUserEntity.getUserid());
            int ret=  sysUserMapper.update(sysUserEntity,updateWrapper);
            return  new ReturnValue(200,"修改成功",ret);
        }catch (Exception ex){
            return  new ReturnValue(500,"修改失败");
        }

    }
    public ReturnValue normallogin(String username,String password){
        int userid=0;
        QueryWrapper<SysUserEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username).eq("password",password);
        List<Map<String, Object>> maps = sysUserMapper.selectMaps(queryWrapper);
        if(maps.size()==0){
            return new ReturnValue(500,"账号密码错误");
        }else {
            for (int i=0; i<maps.size();i++) {
                String pass=(String)maps.get(i).get("password");
                if(pass.equals(password)){
                    userid=  (int)maps.get(i).get("userid");
                    break;
                }
            }
            String jwt = JwtUtil.generateToken(username,userid);
            Map<String,Object> ret=new HashMap<>();
            ret.put("token",jwt);
            ret.put("username",username);
            ret.put("userid",userid);
            return new ReturnValue(200,"",ret);
        }

    }




}
